import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StopBoxArrivalEntry implements StopBoxEntry {
	private static final Color ON_TIME_COLOR = new Color(0, 128, 0);
	private static final Color SCHEDULED_COLOR = Color.BLACK;
	private static final Color LATE_COLOR = Color.BLUE;
	private static final Color EARLY_COLOR = Color.RED;
	private int height = 32;
	private int width = 250;
	private Arrival arr;
	
	public StopBoxArrivalEntry(Arrival arr) {
		this.arr = arr;
		arr.setDestination(this.processHeadsign(arr.getDestination()));
	}
	
	@Override
	public int getHeight() {
		return height;
	}

	@Override
	public void draw(int xPos, int yPos, Graphics g) {
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(xPos, yPos, width, height);
		
		g.setColor(Color.BLACK);
		g.setFont(new Font("Dialog", Font.PLAIN, 20));
		g.drawString("" + arr.getRouteID(), xPos + 2, yPos + 18);
		
		// TODO fix headsign lengths
		//		Keep in mind, some routes are 3 digits
		String headsign = arr.getDestination();
		headsign = processHeadsign(headsign);
		g.setFont(new Font("Dialog", Font.PLAIN, 12));
		g.drawString(headsign, xPos + 7 + 10 * ("" + arr.getRouteID()).length() , yPos + 16);
		
		// Set color for arrivals based on ontime status
		// Scheduled
		if(arr.getOnTimeStatus() == 0)
			g.setColor(SCHEDULED_COLOR);
		// On time
		else if (arr.getOnTimeStatus() == 1)
			g.setColor(ON_TIME_COLOR);
		// Late
		else if (arr.getOnTimeStatus() == 2)
			g.setColor(LATE_COLOR);
		else if (arr.getOnTimeStatus() == 3)
			g.setColor(EARLY_COLOR);
		
		// Draw arrival times
		g.setFont(new Font("Dialog", Font.PLAIN, 20));
		
		if(arr.getMinutesUntilArrival() == 0) {
			g.setFont(new Font("Dialog", Font.PLAIN, 18));
			g.drawString("NOW", xPos + width - 45, yPos + 19);
		} else if (arr.getMinutesUntilArrival() < 10 && arr.getMinutesUntilArrival() > 0) {
			g.drawString("" + arr.getMinutesUntilArrival(), xPos + width - 20, yPos + 18);
		} else {
			g.drawString("" + arr.getMinutesUntilArrival(), xPos + width - 31, yPos + 18);
		}
		
			
	}

	private String processHeadsign(String headsign) {
		headsign = headsignReplacer(headsign, "MARTIN LUTHER KING JUNIOR WAY", "MLK");
		headsign = headsignReplacer(headsign, "MARTIN LUTHER KING JUNIOR", "MLK");
		headsign = headsignReplacer(headsign, "MARTIN LUTHER KING", "MLK");
		headsign = headsignReplacer(headsign, "PLACE", "PL");
		headsign = headsignReplacer(headsign, "PARKWAY", "PKWY");
		headsign = headsignReplacer(headsign, "PARK", "PK");
		headsign = headsignReplacer(headsign, "WAY", "WY");
		headsign = headsignReplacer(headsign, "STREET", "ST");
		headsign = headsignReplacer(headsign, "AVENUE", "AVE");
		headsign = headsignReplacer(headsign, "EAST", "E");
		headsign = headsignReplacer(headsign, "WEST", "W");
		headsign = headsignReplacer(headsign, "NORTH", "N");
		headsign = headsignReplacer(headsign, "SOUTH", "S");
		headsign = headsignReplacer(headsign, "SEATTLE CENTER", "SEA CTR");
		headsign = headsignReplacer(headsign, "UNIVERSITY DISTRICT", "U DIST");
		headsign = headsignReplacer(headsign, "DISTRICT", "DIST");
		headsign = headsignReplacer(headsign, "DOWNTOWN SEATTLE", "DOWNTOWN");
		headsign = headsignReplacer(headsign, "TRANSIT CENTER", "TC");
		headsign = headsignReplacer(headsign, "CENTER", "CTR");
		headsign = headsignReplacer(headsign, "UNIVERSITY", "UNIV");
		headsign = headsignReplacer(headsign, "MOUNT", "MT");
		
		return headsign;
	}
	
	private String headsignReplacer(String headsign, String oldString, String newString) {
		Pattern p = Pattern.compile(oldString);
		Matcher m = p.matcher(headsign);
		headsign = m.replaceAll(newString);
		
		return headsign;
	}

}
